<?php
include("../fpdf/fpdf.php");
include("../db_connect.php");

class PDF extends FPDF
{
	function Header()
	{
		// Logo ('location',x,y,w,h) ;
		//$this->Image('../../images/mlogo.png' ,6,5,20,20);
		// Arial bold 15
		$this->SetFont('Arial','B',30);
		// Move to the right
			
		//set the text color
		$this->SetTextColor(255,0,24);
		// Title cell(x,y,title,border,?,?);
		$this->Cell(4,10,'BasciAutoSales Invoice',0,0,'L');
		$this->Ln();
		// Line break
		$this->Ln(5);
		$this->SetTextColor(0,0,0);
		$this->SetFont('Arial','',14);
		$this->Cell(1,5,'Street:  #36 Poblacion   ',5,0,'L');
		$this->Ln();
		$this->Cell(1,5,'Calumpit Bulacan',5,0,'L');
		$this->Ln();
		$this->Cell(1,5,'Contact no: 6392658956',5,0,'L');
		$this->Ln();
		$this->Cell(1,5,'Email:bascibsu@yahoo.com.ph',5,0,'L');
		$this->Ln();
		$this->Cell(1,5,'website: basciautosales.hol.es',5,0,'L');
		/*user detail*/
		
	}
	function Date_invoice($txn_id){
		$this->SetFont('Arial','B',14);
		$date = date("M-j-Y");
		$this->Cell(180,5,'Date: '.$date.' ',5,0,'R');
		$this->Ln();
		$this->Cell(180,5,'Invoice-id# '.$txn_id.'',5,0,'R');
		$this->Ln();
	}
	
	function Personal_info($id){
	
		$query= mysql_query("SELECT * FROM tbl_account where id='$id'");
		while($row = mysql_fetch_array($query)){
			$name  = $row['fname'] .' '. $row['lname'];
			$address = $row['address'];
			$contact = $row['contacts'];
			$email = $row['email'];
		}
		
		$this->SetTextColor(255,0,24);
		$this->SetFont('Arial','',18);
		$date = date("M-j-Y");
		$this->Cell(1,5,'Member Infomation',5,0,'L');
		$this->Ln(8);
		$this->SetTextColor(0,0,0);
		$this->SetFont('Arial','',14);
		$this->Cell(1,5,'Name: '.strtoupper($name).'',5,0,'L'); 
		$this->Ln();
		$this->Cell(1,5,'Address: '.strtoupper($address).' ',5,0,'L');
		$this->Ln();
		$this->Cell(1,5,'Contact no: '.strtoupper($contact).'',5,0,'L');
		$this->Ln();
		$this->Cell(1,5,'Email: '.strtoupper($email).'',5,0,'L');
			
		$this->Ln(20);
	}
	//displaying total
	function Total($total){
	$this->SetFont('Arial','B',14);
	$this->Cell(180,5,'Total: '.$total.'',5,0,'R');
	$this->Ln();
	}

	// Page footer
	function Footer()
	{
		// Position at 1.5 cm from bottom
		$this->SetY(-15);
		// Arial italic 8
		$this->SetFont('Arial','',8);
		// Page number
		$this->Cell(0,10,'Page '.$this->PageNo().' Thank You For Buying Our Product From Basci Auto Sales',0,0,'C');
		
	}
	
	function LoadData($txn_id){
		
		
		$sql = "SELECT * FROM tblpayment where txn_id = '$txn_id' ";
		
		$result = mysql_query($sql);
		$data = array();
		
		while($row=mysql_fetch_row($result)){ 
			$data[] = $row;
		}
		return $data;
		
		
	}


	//Colored table
	// Colored table
	function FancyTable($header, $data){
		
	//Colors, line width and bold font
			//Colors, line width and bold font
			$this->SetFillColor(70,70,70);
			$this->SetTextColor(255);
			$this->SetDrawColor(128,0,0);
			$this->SetLineWidth(.3);
			$this->SetFont('Arial','B',8);
			$this->SetFont('Arial','',15);
			//Header
			$w=array(25,80,20,30,30);
			$type="";
			for($i=0;$i<count($header);$i++)
		
			$this->Cell($w[$i],10,$header[$i],1,0,'C',true);
			$this->Ln();
			//Color and font restoration
			$this->SetFillColor(225,235,255);
			$this->SetTextColor(0);
			$this->SetFont('Arial','B',8);
			//Data
			$fill=false;
			foreach($data as $row)
			{	
				$price	= $this->getprice($row[3]);
				
				$this->SetFont('Arial','',14);
				$this->Cell($w[0],10,$row[3],'LR',0,'L',$fill);//itemcode
				$this->Cell($w[1],10,$row[4],'LR',0,'L',$fill);//item name
				$this->Cell($w[2],10,$row[5],'LR',0,'L',$fill);//qty
				$this->Cell($w[3],10,$price,'LR',0,'L',$fill);//price
				$this->Cell($w[4],10,$row[8],'LR',0,'L',$fill);//subtotal
				
				$this->Ln();
				
				$fill=!$fill;
			}
			
			$this->Cell(array_sum($w),0,'','T');
		$this->Ln(10);
		
	}
	
	function getprice($id){
		$query = mysql_query("select price from tblswaping where id ='$id'");
		$price = mysql_fetch_array($query);
		return $price['price'];
	}

}

class Sendpdf {
	function run($txn_id,$mc_gross){

	$pdf=new PDF();// call the pdf class where we will create our pdf file	
	
	$total = $mc_gross;
	//get the txn_id to get the user information who buyed the item's
	$sql = mysql_query("SELECT * FROM tblpayment where txn_id='$txn_id'");
	$row1 = mysql_fetch_array($sql);
	$id = $row1['user_id'];//user id of the reciever
	
	//now query it
	$query = mysql_query("SELECT * FROM tbl_account where id='$id'");
	$email = mysql_fetch_array($query);
	
	
	//get the transaction id or invoice id
	//$pdf->header($txn_id);
	
	//assign header of the tbl
	$header=array('Item Code','Item Name','Qty','Price','Line Total');
	//Data loading
	$data=$pdf->LoadData($txn_id);// it query the data need of the file
	$pdf->AddPage();
	$pdf->SetFont('Arial','',10);
	$pdf->Date_invoice($txn_id);
	$pdf->SetFont('Arial','B',10);
	$pdf->Ln();
	$pdf->Personal_info($id);//load the information of the user
	$pdf->Ln();
	$pdf->FancyTable($header,$data);
	$pdf->Ln();
	$pdf->Total($total);
	$pdfdoc = $pdf->Output("", "S");

	$toemail = $email['email'];
	//$toemail = "vince_delafuente027@yahoo.com";//$row['email'];// email of the customer;
	$separator = md5(time());
	$eol = PHP_EOL;
	$filename = "invoice.pdf";
	$attachment = chunk_split(base64_encode($pdfdoc));
		
	$to = $toemail; 
	$from = "admin@basciautosales.zz.mu"; 
	$subject = "Basciautosales Invoce"; 
	$message = "<p>Please see the attachment.</p>";
		
		
	$headers  = "From: ".$from.$eol;
	$headers .= "MIME-Version: 1.0".$eol; 
	$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

	// no more headers after this, we start the body! //

	$body = "--".$separator.$eol;
	$body .= "Content-Transfer-Encoding: 10000bit".$eol.$eol;
	$body .= "Please check the pdf file, For inquires please contact us.".$eol;

	// message
	$body .= "--".$separator.$eol;
	$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
	$body .= "Content-Transfer-Encoding: 10000bit".$eol.$eol;
	$body .= $message.$eol;

	// attachment
	$body .= "--".$separator.$eol;
	$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
	$body .= "Content-Transfer-Encoding: base64".$eol;
	$body .= "Content-Disposition: attachment".$eol.$eol;
	$body .= $attachment.$eol;
	$body .= "--".$separator."--";
	echo $body;
	// send message
	mail($to, $subject, $body, $headers);

	}

}
?>